package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.taptech.doufu.sdk.universalimageloader.core.ImageLoader;
import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.bean.AlbumsBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.StartActivityUtil;

/**
 * Created by Administrator on 2015/10/10.
 */
public class AlbmusAdapter extends BaseListAdapter implements View.OnClickListener
{

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    protected ImageLoader imageLoader = null;
    private AlbumsBean bean;


    public AlbmusAdapter(Context mContext)
    {

        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        NovelHolder novelHolder = null;

        if (convertView == null)
        {
            convertView = mLayoutInflater.inflate(R.layout.novel_rank_fragment_layout, null);
            novelHolder = new NovelHolder();
            novelHolder.mSubjectLayout = (View) convertView.findViewById(R.id.subject_layout);
            novelHolder.image = (SimpleDraweeView) convertView.findViewById(R.id.subject_imageview);
            novelHolder.title = (TextView) convertView.findViewById(R.id.subject_describtion);
            convertView.setTag(novelHolder);
        } else
        {
            novelHolder = (NovelHolder) convertView.getTag();
        }
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, ScreenUtil2.dip2px(7), 0, ScreenUtil2.dip2px(7));
        novelHolder.image.setLayoutParams(layoutParams);
        novelHolder.image.setPadding(0, 0, 0, 0);
        novelHolder.image.setBackgroundResource(R.drawable.biankuang_withno_stroke);
        bean = (AlbumsBean) getDataSource().get(position);
        novelHolder.mSubjectLayout.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                AlbumsBean tempBeanTemp=(AlbumsBean)getDataSource().get(position);
                if(tempBeanTemp!=null){
                    if("24".equals(tempBeanTemp.getType())){
                        Intent intent = new Intent(mContext, BrowseActivity.class);
                        intent.putExtra(Constant.URL, tempBeanTemp.getUrl());
                        mContext.startActivity(intent);
                    }else{
                        StartActivityUtil.albumsOnclick(mContext, (AlbumsBean) getDataSource().get(position));
                    }
                }

            }
        });
        if (bean.getIcon() != null || bean.getCover() != null)
        {
            //ImageManager.loadImage(novelHolder.image, bean.getIcon(), 3.0f);
            ImageManager.loadImage(novelHolder.image, bean.getIcon(), 3.0f, novelHolder.title);
        }

        novelHolder.title.setText(bean.getName());
        // novelHolder.title.setVisibility(View.GONE);
        return convertView;
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.subject_layout:
                StartActivityUtil.albumsOnclick(mContext, bean);
                break;
        }

    }


    public static class NovelHolder
    {

        public TextView title;
        public SimpleDraweeView image;
        public View mSubjectLayout;

    }

}
